<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/20
 * Time: 15:32
 */

namespace app\admin\controller;


use app\admin\model\frontend\ApiM;
use app\admin\model\frontend\LikeM;
use app\admin\model\frontend\MsgM;
use app\admin\model\TagM;
use think\Config;
use think\Controller;

class Frontend extends Controller
{

    protected $tagM;
    protected $msgM;
    protected $likeM;

    protected $novelTag;
    protected $albumTag;
    protected $articleTag;
    /*
   * 依赖注入Model
   * */
    public function __construct(TagM $tagM,MsgM $msgM,LikeM $likeM){
        parent::__construct();
        $this->tagM = $tagM;
        $this->msgM = $msgM;
        $this->likeM = $likeM;
        $this->novelTag = array_keys(Config::get('app.tag.category'))[0];
        $this->albumTag = array_keys(Config::get('app.tag.category'))[1];
        $this->articleTag = array_keys(Config::get('app.tag.category'))[2];
        $this->assign("index",ApiM::getIndex());
    }

    /**
     * 翻页
     * @return mixed
     */
    public function chapter_page(){
        $chapterId = ApiM::getChapterPage();
        $this->assign("novel",ApiM::getNovel(input('get.novel_id/d')));
        $this->assign("chapter",ApiM::getChapterDetail($chapterId));
        return $this->fetch('frontend_page/home/blog/blog_novel_chapter_detail');
    }

    /**
     * 返回章节详细
     * @return mixed
     */
    public function novel_chapter_detail(){
        $this->assign("novel",ApiM::getNovel(input('get.novel_id/d')));
        $this->assign("chapter",ApiM::getChapterDetail(input('get.id/d')));
        return $this->fetch('frontend_page/home/blog/blog_novel_chapter_detail');
    }

    /**
     * 章节列表
     * @return mixed
     */
    public function novel_chapter(){
        $this->assign("novel",ApiM::getNovel(input('get.id/d')));
        $this->assign("chapters",ApiM::getChapter(input('get.id/d')));
        return $this->fetch('frontend_page/home/blog/blog_novel_chapter');
    }

    /**
     * 小说列表
     * @return mixed
     */
    public function novel(){
        $tags = $this->tagM->listAll($this->novelTag)->toArray();
        $this->assign("novel",ApiM::getNovelList($tags));
        return $this->fetch('frontend_page/home/blog/blog_novel');
    }

    /**
     * 用户喜欢
     */
    public function guest_like(){
        return $this->likeM->like();
    }

    /**
     * 用户留言
     * @return array|mixed
     */
    public function guest_msg(){
        return $this->msgM->add();
    }

    /**
     * 文章分类列表
     * @return mixed
     */
    public function article(){
        $tags = $this->tagM->listAll($this->articleTag);
        if (count($tags))
            $tag = input('get.tag/s',$tags->toArray()[0]['code']);
        else
            $tag='none';
        $this->assign("tag",$tag);
        $this->assign("tags",$tags);
        $this->assign("article",ApiM::listArticleByTag($tag));
        return $this->fetch('frontend_page/home/blog/blog_article');
    }

    /*
     *  详细
     * */
    public function article_detail(){
        $id = input('get.id/d');
        $this->assign("likes",ApiM::articleLikes($id));
        $this->assign("like",ApiM::articleLikeById($id));
        $this->assign("article",ApiM::getArticle($id));
        return $this->fetch('frontend_page/home/blog/blog_detail');
    }

    /**
     * motto 瀑布流
     * @return array
     */
    public function time_flow(){
        return ApiM::getTimeFlow();
    }

    /**
     * 博客中心
     * @return mixed
     */
    public function blog(){
        $this->assign("article",ApiM::getArticleNew());
        $this->assign("timeline",ApiM::getTimeLineNew());
        return $this->fetch('frontend_page/home/blog/blog_index');
    }

    /**
     * 首页
     * @return mixed
     */
    public function index(){
        $this->assign("album",ApiM::getAlbum());
        $this->assign("motto",ApiM::getLastMotto());
        return $this->fetch('frontend_page/home/index');
    }

    /*
     *  记忆回廊
     * */
    public function album(){
        $tags = $this->tagM->listAll($this->albumTag);
        if (count($tags))
            $tag = input('get.tag/s',$tags->toArray()[0]['code']);
        else
            $tag='none';
        $this->assign("tag",$tag);
        $this->assign("tags",$tags);
        $this->assign("data",ApiM::listAlbumByTag($tag));
        return $this->fetch('frontend_page/home/album');
    }


}